package view.test;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

import util.SpriteUtils;
import view.entities.ViewEntity;
import controller.GameController;
import controller.IView;

public class MainPanel extends JPanel implements IView {
	
	private static final long serialVersionUID = 4141687358448883457L;
	private static final Dimension MIN_LIST_SIZE = new Dimension(192, 192);
	private static final Point LIST_LOCATION = new Point(608, 208);
	private static final Point LOAD_BTTN_LOCATION = new Point(660, 408);
	private static final Dimension BUTTON_SIZE = new Dimension(80, 40);
	private static final Point ADDITIVE_COLOR_LOC = new Point(638, 458);
	
	private static final Color LOAD_BTTEN_COLOR = Color.ORANGE.brighter();
	private static final Color LIST_BG_COLOR = Color.CYAN.brighter();
	
	private GameBoardPanel gameBoardPanel;
	private ViewBin viewBin;
	private GlassPane glassPane;
	private JList levelList;
	private GameController controller;
	
	public MainPanel() {
		this.setVisible(true);
		this.setMinimumSize(new Dimension(800, 600));
		this.setLayout(null);
		
		ImageIcon additiveColor = SpriteUtils.loadSprite("/AdditiveColor.png", this);
		JLabel additiveLabel = new JLabel(additiveColor);
		additiveLabel.setLocation(ADDITIVE_COLOR_LOC);
		additiveLabel.setSize(additiveColor.getIconWidth(), additiveColor.getIconHeight());
		this.add(additiveLabel);
		
		setUpLevelList();
		
		this.setBackground(Color.ORANGE);
		
		this.addMouseMotionListener(new MouseMotionListener() {
			
			@Override
			public void mouseMoved(MouseEvent e) {
				Component component = e.getComponent();
				Point glassPanePoint = SwingUtilities.convertPoint(component, e.getPoint(), glassPane);
				glassPane.setCursorPoint(glassPanePoint);
				glassPane.repaint();
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
				Component component = e.getComponent();
				Point glassPanePoint = SwingUtilities.convertPoint(component, e.getPoint(), glassPane);
				glassPane.setCursorPoint(glassPanePoint);
				glassPane.repaint();
			}
		});
	}
	
	private void setUpLevelList() {
		
		levelList = new JList();
		levelList.setMinimumSize(MIN_LIST_SIZE);
		levelList.setSize(MIN_LIST_SIZE);
		levelList.setBackground(LIST_BG_COLOR);
		levelList.setVisible(true);
		
		JScrollPane scrollPane = new JScrollPane(levelList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setLocation(LIST_LOCATION);
		scrollPane.setSize(MIN_LIST_SIZE);
		scrollPane.setVisible(true);

		this.add(scrollPane);
		
		JButton loadButton = new JButton("Load");
		loadButton.setVisible(true);
		loadButton.setLocation(LOAD_BTTN_LOCATION);
		loadButton.setSize(BUTTON_SIZE);
		loadButton.setBackground(LOAD_BTTEN_COLOR);
		
		this.add(loadButton);
		
		MouseInputListener listener = new MouseInputListener() {
			
			@Override
			public void mouseMoved(MouseEvent e) {
				Component component = e.getComponent();
				Point glassPanePoint = SwingUtilities.convertPoint(component, e.getPoint(), glassPane);
				glassPane.setCursorPoint(glassPanePoint);
				glassPane.repaint();
			}
			
			@Override
			public void mouseDragged(MouseEvent e) {
				Component component = e.getComponent();
				Point glassPanePoint = SwingUtilities.convertPoint(component, e.getPoint(), glassPane);
				glassPane.setCursorPoint(glassPanePoint);
				glassPane.repaint();
			}
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseClicked(MouseEvent arg0) {
				controller.handleLevelLoadAction((String)levelList.getSelectedValue());
			}
		};
		
		loadButton.addMouseListener(listener);
		loadButton.addMouseMotionListener(listener);
		levelList.addMouseMotionListener(listener);
		
	}
	public GameBoardPanel getGameBoardPanel() {
		return gameBoardPanel;
	}

	public void setGameBoardPanel(GameBoardPanel gameBoardPanel) {
		if (this.gameBoardPanel != null)
			this.remove(this.gameBoardPanel);
		
		this.gameBoardPanel = gameBoardPanel;
		this.add(gameBoardPanel);
	}

	public ViewBin getViewBin() {
		return viewBin;
	}

	public void setViewBin(ViewBin viewBin) {
		if (this.viewBin != null)
			this.remove(this.viewBin);
		
		this.viewBin = viewBin;
		this.add(viewBin);
	}

	@Override
	public void setCursorToIconAtCell(Point cell) {
		ViewEntity entity = gameBoardPanel.getGameBoard()[cell.x][cell.y];
		glassPane.setCursorImage(entity.getEntityIcon().getImage());
	}

	@Override
	public void setGridOn(boolean isOn) {
		getGameBoardPanel().setGridOn(isOn);
	}

	@Override
	public void resetCursor() {
		glassPane.setCursorImage(null);
		glassPane.repaint();
	}

	@Override
	public void setCursorIcon(ImageIcon image) {
		glassPane.setCursorImage(image.getImage());
	}

	public GlassPane getGlassPane() {
		return glassPane;
	}

	public void setGlassPane(GlassPane glassPane) {
		this.add(glassPane);
		this.setComponentZOrder(glassPane, 0);
		this.glassPane = glassPane;
	}
	
	@Override
	public void setLevelNameList(String[] levelNameList) {
		this.levelList.setListData(levelNameList);
	}

	@Override
	public void setController(GameController controller) {
		this.controller = controller;
	}

	@Override
	public void laserAnimationOn(boolean isOn) {
		gameBoardPanel.setLaserAnimationOn(isOn);
	}
		
}
